package saveData;

import model.ApiDataModel;
import model.TestResultAnalysisModel;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

/**
 * Created by yangxianming on 2015-06-24.
 */
public class Save2txt implements SaveDataImpl {
    String resultFilePath = null;
    String resultFilePathFull = null;
    PrintWriter print = null;
    PrintWriter printWritertFailed = null;  //这个用来创建
    BufferedWriter bufferedWritertFailed = null;    //这个用来追加
    String resultValue = "";
    String filename = "";

    public Save2txt(String filename, String resultFilePath){
//        创建结果记录文件
        this.filename = filename;
        this.resultFilePath = resultFilePath;
        createFile(filename);
    }

    @Override
    public void save(ArrayList<ApiDataModel> list) {
        boolean remarkApi = false;
        StringBuilder resultStringTotal = new StringBuilder();    //这里记录所有的数据
        StringBuilder resultStringTotalFailed = new StringBuilder();    //这里记录所有的失败数据

        for (ApiDataModel adm:list){
            StringBuilder resultString = new StringBuilder(); //写入一个api数据
//            apiId写入
            resultString.append("Test apiId: " + adm.apiId + "\n");
//            url
            resultString.append("Test url: " + adm.method + " " + adm.domain + adm.url + "\n");
//            hearder
            resultString.append("Test header: [");
            for (Map.Entry<String, String> me:adm.header.entrySet()){
                resultString.append("{k:" + me.getKey() + " ");
                resultString.append("v:" + me.getValue() + "}, ");
            }
            resultString.append("]\n");
//            parameter
            resultString.append("Test parameter: [");
            for (Map.Entry<String, String> me:adm.parameters.entrySet()){
                resultString.append("{k:" + me.getKey() + " ");
                resultString.append("v:" + me.getValue() + "}, ");
            }
            resultString.append("]\n");
//            return header
            resultString.append("Test return header: [");
            for (Map.Entry<String, java.util.List<String>> me:adm.apiResponseDataModel.responseHeader.entrySet()){
                String k = me.getKey();
                List v = me.getValue();
                resultString.append("{k:" + me.getKey() + " v:");
                for (Object ov:v){
                    resultString.append(ov + ", ");
                }
                resultString.append("}");
            }
            resultString.append("]\n");
//            returnBody
            resultString.append("Test returnBody:" + adm.apiResponseDataModel.responseBody + "\n");
//            result,descript,expect
            resultString.append("Test descript: [\n");
            for (TestResultAnalysisModel testResultAnalysisModel:adm.testResultAnalysisModelList){
//                result
                resultValue = "";
                if (testResultAnalysisModel.result != null)
                    resultValue = testResultAnalysisModel.result ? "PASS" : "FAILED";
                resultString.append("\t" + "Test result: " + resultValue + " ; ");
//                descript
                resultString.append("Test descript: " + testResultAnalysisModel.describe + " ; ");
//                expect
                resultString.append("Test expect: " + testResultAnalysisModel.expect + " ; ");
                resultString.append("\n");
//                这里将 失败的 或者 结果为空 的api做标记,稍等记录到失败日志中
                if (testResultAnalysisModel.result == null || !testResultAnalysisModel.result)
                    remarkApi = true;
            }
            resultString.append("]\n");
            resultString.append("\n");
            resultStringTotal.append(resultString);
            if (remarkApi){
                resultStringTotalFailed.append("FileName: " + filename + "\n");
                resultStringTotalFailed.append(resultString);
                remarkApi = false;
            }
        }
        print.write(resultStringTotal.toString());
        print.close();

        String data = resultStringTotalFailed.toString();
        try {
            bufferedWritertFailed.write(data);
            bufferedWritertFailed.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    private void createFile(String filename){
//        获取日期作为目录名 每次测试将会创建一个文件夹保存测试结果
        Date date = new Date();

//        修改data字符串,可以用户创建目录和文件
        String mydata = date.toString().replaceAll(":", "-");
//        创建文件,与记录失败请求的文件
        resultFilePathFull = resultFilePath + "/"+ filename + "-" + mydata;
        File file = new File(resultFilePathFull);
        File fileFailed = new File(resultFilePath + "\\failedApi");
        try {
            print = new PrintWriter(file);
            FileWriter fileWriter = new FileWriter(fileFailed.getAbsolutePath(), true);
            bufferedWritertFailed = new BufferedWriter(fileWriter);
        }catch (IOException e){
            e.printStackTrace();
        }
    }


}
